package zhongbo.java.air.dictionary.service.Impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhongbo.java.air.common.exception.MyException;
import zhongbo.java.air.common.vo.PageVo;
import zhongbo.java.air.dictionary.mapper.AirPortMapper;
import zhongbo.java.air.dictionary.model.AirPort;
import zhongbo.java.air.dictionary.service.AirPortService;

import java.util.Date;

@Service
public class AirPortServiceImpl implements AirPortService {
    @Autowired
    private AirPortMapper airPortMapper;

    @Override
    public AirPort addAirPort(AirPort airPort) throws MyException {
        Date dNow = new Date();
//        SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd hh:mm:ss");
        airPort.setCreateTime(dNow);
        airPort.setUpdateTime(dNow);
        AirPort airPort1 = airPortMapper.selectByIcaoCode(airPort.getIcaoCode());
        if (airPort.getStatus().matches("^(OPERATIONAL|CLOSED|RESTRICTED)$")) {
            if (airPort1 == null) {
                int ok = airPortMapper.insert(airPort);
                if (ok <= 0) {
                    throw new MyException("机场信息添加失败");
                }
                return airPortMapper.selectByIcaoCode(airPort.getIcaoCode());
            } else {
                throw new MyException("ICAO代码已存在");
            }
        }
        throw new MyException("状态值错误");
    }

    @Override
    public String deleteAirPort(String id) throws MyException {
        int ok = airPortMapper.deleteById(id);
        if (ok <= 0) {
            throw new MyException("机场信息删除失败");
        }
        return String.valueOf(ok);
    }

    @Override
    public AirPort updateAirPort(AirPort airPort) throws MyException {
        if (airPort.getStatus() != null) {
            if (!airPort.getStatus().matches("^(OPERATIONAL|CLOSED|RESTRICTED)$")) {
                throw new MyException("状态值错误");
            }
        }
        Date dNow = new Date();
        airPort.setUpdateTime(dNow);
        int ok = airPortMapper.updateById(airPort);
        if (ok <= 0) {
            throw new MyException("机场信息修改失败");
        }
        return airPortMapper.selectById(airPort.getId());
    }

    @Override
    public AirPort selectAirPortById(String id) {
        return airPortMapper.selectById(id);
    }

    @Override
    public PageVo<AirPort> page(Integer pageNumber, Integer pageSize, boolean order, String searchField, String keyword, String sortField) {
        QueryWrapper<AirPort> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(keyword) && StrUtil.isNotBlank(searchField)) {
            String[]fieldsArray = searchField.split(",");
            for (String field : fieldsArray) {
                queryWrapper.or().like(StrUtil.toUnderlineCase(field), keyword);
            }
        }
        if (StrUtil.isNotBlank(sortField)) {
            if (order) {
                queryWrapper.orderByAsc(sortField);
            } else {
                queryWrapper.orderByDesc(sortField);
            }
        }

        IPage<AirPort> pageParam = new Page<>(pageNumber, pageSize);
        IPage<AirPort> resultPage = airPortMapper.selectPage(pageParam, queryWrapper);
        return new PageVo<>(resultPage);
    }
}
